package com.ruoyi.blog.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.AcCollection;
import com.ruoyi.blog.domain.AcLike;
import com.ruoyi.blog.service.IAcCollectionService;
import com.ruoyi.blog.service.IAcLikeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.blog.domain.AcBlog;
import com.ruoyi.blog.service.IAcBlogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 *
 * @author ruoyi
 * @date 2022-06-19
 */
@RestController
@RequestMapping("/blog/blog")
public class AcBlogController extends BaseController
{
    @Autowired
    private IAcBlogService acBlogService;

    @Autowired
    private IAcLikeService acLikeService;

    @Autowired
    private IAcCollectionService acCollectionService;


    @GetMapping("/getBlogList")
    public AjaxResult getBlogList()
    {
        AcBlog acBlog = new AcBlog();
        acBlog.setUserId(getUserId());
        acBlog.setStatus(1);
        List<AcBlog> list = acBlogService.selectAcBlogList(acBlog);
        Collections.sort(list, (o1, o2) -> {
            if (o1.getIsTop() != o2.getIsTop()) return o2.getIsTop() - o1.getIsTop();
            return o2.getCreateTime().compareTo(o1.getCreateTime());
        });
        for (AcBlog blog : list) {
            String s = blog.getContent();
            String regEx_html="<[^>]+>";
            Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            Matcher m_html = p_html.matcher(s);
            s = m_html.replaceAll("").substring(0, Math.min(s.length(), 200));
            blog.setContent(s);
        }
        return AjaxResult.success(list);
    }

    /**
     * 获取前5条推荐博客
     */
    @GetMapping("/getRecommend")
    public AjaxResult getRecommend()
    {
        AcBlog acBlog = new AcBlog();
        acBlog.setUserId(getUserId());
        acBlog.setStatus(1);
        List<AcBlog> list = acBlogService.selectAcBlogList(acBlog);
        for (AcBlog blog : list)
            blog.setContent(null);
        Collections.sort(list, (o1, o2) -> (int)(o2.getViews() - o1.getViews()));
        while (list.size() > 3)
            list.remove(list.size() - 1);
        return AjaxResult.success(list);
    }

    /**
     * 获取博客详情
     */
    @GetMapping("/getDetail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        AcBlog blog = acBlogService.selectAcBlogById(id);
        blog.setViews(blog.getViews() + 1);
        acBlogService.updateAcBlog(blog);

        AcLike like = new AcLike();
        like.setObjType(0l);
        like.setObjId(blog.getId());
        like.setIsLike(1);
        like.setUserId(getUserId());
        blog.setLike(acLikeService.selectAcLikeList(like).size() > 0);
        like.setUserId(null);
        blog.setLikeCount(acLikeService.selectAcLikeList(like).size());

        AcCollection collection = new AcCollection();
        collection.setUserId(getUserId());
        collection.setBlogId(blog.getId());
        blog.setCollect(acCollectionService.selectAcCollectionList(collection).size() > 0);
        return AjaxResult.success(blog);
    }

    /**
     * 查询文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcBlog acBlog)
    {
        startPage();
        List<AcBlog> list = acBlogService.selectAcBlogList(acBlog);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcBlog acBlog)
    {
        List<AcBlog> list = acBlogService.selectAcBlogList(acBlog);
        ExcelUtil<AcBlog> util = new ExcelUtil<AcBlog>(AcBlog.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(acBlogService.selectAcBlogById(id));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcBlog acBlog)
    {
        return toAjax(acBlogService.insertAcBlog(acBlog));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcBlog acBlog)
    {
        return toAjax(acBlogService.updateAcBlog(acBlog));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acBlogService.deleteAcBlogByIds(ids));
    }
}
